package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.biaoju.UserBiaoJuData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class UserBiaoJuDaoImpl extends TempDataObjectDao<UserBiaoJuData> {

    public static String getUserBiaoJuDataSql = "select * from `t_u_biaoju` where `activityId`=? and `userId`=?";
    public UserBiaoJuData getUserBiaoJuData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserBiaoJuDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserBiaoJuDataSql = "update `t_u_biaoju` set refreshAmount=?,guaranteeAmount=?,lastRefreshTime=?,money=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(UserBiaoJuData userBiaoJuData) {
        userBiaoJuData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userBiaoJuData.getRefreshAmount()));
        para.put(i++, new DbParameter(Types.INTEGER, userBiaoJuData.getGuaranteeAmount()));
        para.put(i++, new DbParameter(Types.BIGINT, userBiaoJuData.getLastRefreshTime()));
        para.put(i++, new DbParameter(Types.BIGINT, userBiaoJuData.getMoney()));
        para.put(i++, new DbParameter(Types.INTEGER, userBiaoJuData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userBiaoJuData.getUserId()));
        if (getDb().executeNoneQuery(updateUserBiaoJuDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserBiaoJuDataSql = "insert into `t_u_biaoju`(activityId,userId,refreshAmount,guaranteeAmount,lastRefreshTime,money) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserBiaoJuData userBiaoJuData) {
        userBiaoJuData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userBiaoJuData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userBiaoJuData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userBiaoJuData.getRefreshAmount()));
        para.put(i++, new DbParameter(Types.INTEGER, userBiaoJuData.getGuaranteeAmount()));
        para.put(i++, new DbParameter(Types.BIGINT, userBiaoJuData.getLastRefreshTime()));
        para.put(i++, new DbParameter(Types.BIGINT, userBiaoJuData.getMoney()));
        if (getDb().executeNoneQuery(insertUserBiaoJuDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserBiaoJuData parseData(ResultSet rs) throws SQLException {
        UserBiaoJuData userBiaoJuData = new UserBiaoJuData();
        userBiaoJuData.setActivityId(rs.getInt("activityId"));
        userBiaoJuData.setUserId(rs.getLong("userId"));
        userBiaoJuData.setRefreshAmount(rs.getInt("refreshAmount"));
        userBiaoJuData.setGuaranteeAmount(rs.getInt("guaranteeAmount"));
        userBiaoJuData.setLastRefreshTime(rs.getLong("lastRefreshTime"));
        userBiaoJuData.setMoney(rs.getLong("money"));

        return userBiaoJuData;
    }

}
